import 'dart:async';

import 'package:flutter/cupertino.dart';
import 'package:get/get.dart';
import 'package:smart_translator/app/utils/CommonUtil.dart';

import '../../../event/DataCallBack.dart';
import '../../../nets/NetWorkManager.dart';
import '../../../utils/tips.dart';

class ValidateCodeController extends GetxController {

  var focusNode1 = FocusNode().obs;
  var focusNode2 = FocusNode().obs;
  var focusNode3 = FocusNode().obs;
  var focusNode4 = FocusNode().obs;
  var focusNode5 = FocusNode().obs;
  var focusNode6 = FocusNode().obs;

  var textEditor1 = TextEditingController().obs;
  var textEditor2 = TextEditingController().obs;
  var textEditor3 = TextEditingController().obs;
  var textEditor4 = TextEditingController().obs;
  var textEditor5 = TextEditingController().obs;
  var textEditor6 = TextEditingController().obs;

  var hiddenUnderLine1 = false.obs;
  var hiddenUnderLine2 = false.obs;
  var hiddenUnderLine3 = false.obs;
  var hiddenUnderLine4 = false.obs;
  var hiddenUnderLine5 = false.obs;
  var hiddenUnderLine6 = false.obs;

  var account = ''.obs;
  var password = ''.obs;
  var isEmailType =  false.obs;

  var isBtnEnable = false.obs;

  var timeCount = 60.obs;
  var showTimeText = false.obs;
  Timer? _timer;

  @override
  void onInit() {
    super.onInit();
    account.value = Get.arguments['account'] as String;
    password.value = Get.arguments['passwd'] as String;
    timeCount.value =60;
    showTimeText.value = false;
    isEmailType.value = CommonUtil.isEmail(account.value);

  }

  @override
  void onReady() {
    super.onReady();
  }

  @override
  void onClose() {
    super.onClose();
  }

  void changeFocus() {
    hiddenUnderLine1.value =  textEditor1.value.text.trim() == '' ? false: true;
    hiddenUnderLine2.value =  textEditor2.value.text.trim() == '' ? false : true;
    hiddenUnderLine3.value =  textEditor3.value.text.trim() == '' ? false : true;
    hiddenUnderLine4.value =  textEditor4.value.text.trim() == '' ? false : true;
    hiddenUnderLine5.value =  textEditor5.value.text.trim() == '' ? false : true;
    hiddenUnderLine6.value =  textEditor6.value.text.trim() == '' ? false : true;

    if (textEditor1.value.text == '') {
      focusNode1.value.requestFocus();
    }

    if (textEditor1.value.text != '' && textEditor2.value.text == '') {
      focusNode1.value.unfocus();
      focusNode2.value.requestFocus();
    }

    if (textEditor2.value.text != '' && textEditor3.value.text == '') {
      focusNode2.value.unfocus();
      focusNode3.value.requestFocus();
    }

    if (textEditor3.value.text != '' && textEditor4.value.text == '') {
      focusNode3.value.unfocus();
      focusNode4.value.requestFocus();
    }

    if (textEditor4.value.text != '' && textEditor5.value.text == '') {
      focusNode4.value.unfocus();
      focusNode5.value.requestFocus();
    }

    if (textEditor5.value.text != '' && textEditor6.value.text == '') {
      focusNode5.value.unfocus();
      focusNode6.value.requestFocus();
    }

    if (textEditor6.value.text != '') {

      String validateCode = textEditor1.value.text+textEditor2.value.text+textEditor3.value.text+textEditor4.value.text+
          textEditor5.value.text+textEditor6.value.text;
      if (isEmailType.value) {
        NetWorkManager.getInstance().requestBussiness(NetWorkManager.SEND_EMAIL_REGISTER, {
          'email': account.value,
          'password': password.value,
          'vcode':validateCode

        }, NetWorkManager.methodPOST, DataCallBack(
            businessCallBack: (v) {
              Tips.showToast('注册成功');
              isBtnEnable.value = true;
            },
            errorCallBack: (v) {
              isBtnEnable.value = false;
              Tips.showToast(v);
            }

        ),showLoading: true);


      } else {

        NetWorkManager.getInstance().requestBussiness(NetWorkManager.SEND_PHONE_REGISTER, {
          'phone': account.value,
          'password': password.value,
          'vcode':validateCode

        }, NetWorkManager.methodPOST, DataCallBack(
            businessCallBack: (v) {
              Tips.showToast('注册成功');
              isBtnEnable.value = true;
            },
            errorCallBack: (v) {
              isBtnEnable.value = false;
              Tips.showToast(v);
            }

        ),showLoading: true);


      }

    }
  }


  void reTryValidateCode() {
    if (isEmailType.value) { //邮箱.
      NetWorkManager.getInstance().requestBussiness(NetWorkManager.SEND_EMAIL_VERIFICATION_CODE, {
        'email': account.value,
        'codemode': 1

      }, NetWorkManager.methodPOST, DataCallBack(
          businessCallBack: (v) {
             Tips.showToast('验证码已发送');
             showTimeText.value = true;
             _timer = Timer.periodic(Duration(seconds: 1), (timer) {
               if (timeCount.value <= 1) {
                 timer.cancel();
                 showTimeText.value = false;
                 timeCount.value = 60;
               } else {
                 timeCount.value = timeCount.value - 1;
               }
             });
          },
          errorCallBack: (v) {
            Tips.showToast(v);
          }

      ),showLoading: true);


    } else {

      NetWorkManager.getInstance().requestBussiness(NetWorkManager.SEND_PHONE_VERIFICATION_CODE, {
        'phone': account.value,
        'smsmode': 1

      }, NetWorkManager.methodPOST, DataCallBack(
          businessCallBack: (v) {
            Tips.showToast('验证码已发送');
            showTimeText.value = true;
            _timer = Timer.periodic(Duration(seconds: 1), (timer) {
              if (timeCount.value < 1) {
                timer.cancel();
                showTimeText.value = false;
                timeCount.value = 60;
              } else {
                timeCount.value = timeCount.value - 1;
              }
            });
          },
          errorCallBack: (v) {
            Tips.showToast(v);
          }

      ),showLoading: true);



    }
  }

}
